﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000322 RID: 802
	internal class ThrottleFirstObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x060010BC RID: 4284 RVA: 0x0004D0B3 File Offset: 0x0004B4B3
		public ThrottleFirstObservable(IObservable<T> source, TimeSpan dueTime, IScheduler scheduler) : base(scheduler == Scheduler.CurrentThread || source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.dueTime = dueTime;
			this.scheduler = scheduler;
		}

		// Token: 0x060010BD RID: 4285 RVA: 0x0004D0E4 File Offset: 0x0004B4E4
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new ThrottleFirstObservable<T>.ThrottleFirst(this, observer, cancel).Run();
		}

		// Token: 0x040009D6 RID: 2518
		private readonly IObservable<T> source;

		// Token: 0x040009D7 RID: 2519
		private readonly TimeSpan dueTime;

		// Token: 0x040009D8 RID: 2520
		private readonly IScheduler scheduler;

		// Token: 0x02000323 RID: 803
		private class ThrottleFirst : OperatorObserverBase<T, T>
		{
			// Token: 0x060010BE RID: 4286 RVA: 0x0004D0F3 File Offset: 0x0004B4F3
			public ThrottleFirst(ThrottleFirstObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x060010BF RID: 4287 RVA: 0x0004D118 File Offset: 0x0004B518
			public IDisposable Run()
			{
				this.cancelable = new SerialDisposable();
				IDisposable disposable = this.parent.source.Subscribe(this);
				return StableCompositeDisposable.Create(this.cancelable, disposable);
			}

			// Token: 0x060010C0 RID: 4288 RVA: 0x0004D150 File Offset: 0x0004B550
			private void OnNext()
			{
				object obj = this.gate;
				lock (obj)
				{
					this.open = true;
				}
			}

			// Token: 0x060010C1 RID: 4289 RVA: 0x0004D190 File Offset: 0x0004B590
			public override void OnNext(T value)
			{
				object obj = this.gate;
				lock (obj)
				{
					if (!this.open)
					{
						return;
					}
					this.observer.OnNext(value);
					this.open = false;
				}
				SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
				this.cancelable.Disposable = singleAssignmentDisposable;
				singleAssignmentDisposable.Disposable = this.parent.scheduler.Schedule(this.parent.dueTime, new Action(this.OnNext));
			}

			// Token: 0x060010C2 RID: 4290 RVA: 0x0004D22C File Offset: 0x0004B62C
			public override void OnError(Exception error)
			{
				this.cancelable.Dispose();
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x060010C3 RID: 4291 RVA: 0x0004D294 File Offset: 0x0004B694
			public override void OnCompleted()
			{
				this.cancelable.Dispose();
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x040009D9 RID: 2521
			private readonly ThrottleFirstObservable<T> parent;

			// Token: 0x040009DA RID: 2522
			private readonly object gate = new object();

			// Token: 0x040009DB RID: 2523
			private bool open = true;

			// Token: 0x040009DC RID: 2524
			private SerialDisposable cancelable;
		}
	}
}
